package com.jingwei.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 经纬
 * @since 2025/1/2 13:11
 */
@SpringBootTest
public class testActivitiVar {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;


    /**
     * 部署流程
     */
    @Test
    public void deploy() {


        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3、使用service进行流程的部署，部署bpmn和png
        Deployment deploy = repositoryService.createDeployment()
                .name("三天判断OA审批")
                .addClasspathResource("bpmn/oa.bpmn20.xml")
//                .addClasspathResource("bpmn/cc.bpmn20.png")
                .deploy();
    }


    /**
     * 查询已部署的流程
     */
    @Test
    public void ActivitiList() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<Deployment> list = repositoryService.createDeploymentQuery().list();
        list.forEach(deployment -> {
            System.out.println("流程部署id：" + deployment.getId());
            System.out.println("流程部署名称：" + deployment.getName());//bd3dbfee-c8d3-11ef-b5a6-fe854003188c
            System.out.println(deployment.getKey());
            System.out.println(deployment.getTenantId());
        });
    }

    // 查询流程定义
    @Test
    public void getDeployment() {
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .list();
        for (ProcessDefinition pd : list) {
            System.out.println("-----------------");
            // 获取名字
            System.out.println("Name: " + pd.getName());
            // 获取key
            System.out.println("Key: " + pd.getKey());
            // 获取资源名称
            System.out.println("ResourceName: " + pd.getResourceName());
            // 获取id
            System.out.println("DeploymentId: " + pd.getDeploymentId());
            // 获取版本
            System.out.println("Version: " + pd.getVersion());
        }
    }


    /**
     * 删除流程
     */

    @Test
    public void deleteActiviti() {
        //删除部署
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.deleteDeployment("707bee5e-cbf9-11ef-94c0-fa48906c19ec", true);
        //bd3dbfee-c8d3-11ef-b5a6-fe854003188c
    }

    ///***************************************
    //初始化流程实例
    @Test
    public void init() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("leader", "部门领导名称");
        //1、创建（流程引擎）ProcessEngine
        ProcessInstance test = runtimeService.startProcessInstanceByKey("oa", "3dayOA", variables);
        String processDefinitionId = test.getProcessDefinitionId();
        System.out.println("流程定义id：" + processDefinitionId);

    }

    @Test
    public void taskList() {
        List<Task> list = taskService.createTaskQuery().list();
        list.forEach(a -> {
            System.out.println(a.toString());
        });
    }


    @Test
    public void myTaskList() {
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("技术总监名称")
                .list();

        for (Task task : list) {
            System.out.println("------------------------");
            System.out.println("任务id：" + task.getId());
            System.out.println("环节名称：" + task.getName());
            System.out.println("任务执行人Assignee： " + task.getAssignee());
            System.out.println("时间： " + task.getCreateTime());
        }

    }

    /**
     * 条件审批通过
     */
    @Test
    public void completeTask() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("day", 4);
        variables.put("bigLeader", "技术总监名称");
        taskService.complete("0963bb95-cbfd-11ef-8468-fa48906c19ec", variables);
    }

    /**
     * 查看八戒
     */
    @Test
    public void queryBaj() {
        List<Task> bajTask = taskService.createTaskQuery().taskAssignee("八戒").list();
        bajTask.forEach(ba -> System.out.println(ba.toString()));
    }

    /**
     * 八戒通过
     */
    @Test
    public void completeTaskBaj() {
        taskService.complete("adb431f1-c8e8-11ef-8516-fecd14fc87a2");
    }


    /**
     * 查看任务
     */
    @Test
    public void query() {

        List<Task> tasks = taskService.createTaskQuery().taskName("部门领导审批").list();
        System.out.println(tasks.toString());

    }

    @Test
    public void complete() {

        taskService.complete("e8e9d1e7-cbed-11ef-a32d-fa48906c19ec");

    }


    @Test
    public void completeVar() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("agree", 0);
        taskService.complete("58e6a67d-cbfd-11ef-b725-fa48906c19ec", variables);
    }


    @Test
    public void taskList2() {
        List<Task> list = taskService.createTaskQuery().list();
        System.out.println(list.toString());


    }

    static class intWrap {
        int x;
    }
    public static class Main {
        public static void main(String[] args) {
            intWrap i = new intWrap();
            i.x = 10;
            intWrap j = new intWrap();
            j.x = 20;
            swap(i, j);
            System.out.println("i.x = " + i.x + ", j.x = " + j.x);
        }
        public static void swap(intWrap i, intWrap j) {
            int temp = i.x;
            i.x = j.x;
            j.x = temp;
        }
    }

}